package com.example.app;

import com.alibaba.fastjson.JSONObject;
import com.example.app.annotaions.App;
import com.example.app.model.AppModel;
import com.example.auth.model.UserModel;
import com.example.db.model.IPage;
import com.example.utils.ClazzScanUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Classname AppUtils
 * @Description TODO
 * @Date 2022/8/4 10:08
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class AppUtils {

    //app黑名单
    private static List<String> AppIds = new ArrayList<>();


    private static Set<AppModel> appModelSet=new HashSet<AppModel>();

    /**
     * 获取应用分页
     * @param pageNum
     * @param pageSize
     * @param appName
     * @return
     */
    public static IPage<AppModel> getAppModel(int pageNum,int pageSize,String appName){
        List<AppModel> appModels=new ArrayList<>();
        appModels.addAll(appModelSet);
        if(appName!=null){
            appModels=appModels=appModels.stream().filter(e->{
                return e.getAppName().contains(appName);
            }).collect(Collectors.toList());
        }
        List<AppModel> appModels1=appModels.stream().skip((pageNum-1)*pageSize).limit(pageSize).collect(Collectors.toList());
        IPage<AppModel> iPage=new IPage<>(appModels.size(),appModels1);
        return iPage;
    }


    /**
     * 判断此应用是否能被加载
     * @return
     */
    public static boolean isCanLoadJars(List<Class<?>> classes,String fileName,String path){
        if(classes==null || classes.isEmpty()){
            AppModel appModel=getAppModelByPath(path);
            if(appModel != null){
                appModel.setRunInfo("加载失败");
            }
            return false;
        }
        for (int i = 0; i < classes.size() ; i++) {
            Class< ? > clazz = classes.get(i);
            App app=clazz.getAnnotation(App.class);
            if(app!=null){
                if(AppIds.contains(app.AppID())){
                   continue;
                }
                AppModel appModel=build(app.AppID(),app.value(),fileName,app.author(),app.description(),path);
                appModel.setLoadDate(new Date());
                appModelSet.add(appModel);
                return true;
            }
        }
        AppModel appModel=getAppModelByPath(path);
        if(appModel != null){
            appModel.setRunInfo("加载失败");
        }
        //始终没有加载到处理
        return false;
    }

    /**
     * 因为文件路径是唯一的，所以通过路径来辨别
     * @return
     */
    public static AppModel getAppModelByPath(String path){
        for (AppModel app : appModelSet) {
            if(app.getPath().equals(path)){
                return app;
            }
        }
        return null;
    }

    public static AppModel getAppByAppId(String appId){
        for (AppModel app : appModelSet) {
            if(app.getAppId().equals(appId)){
                return app;
            }
        }
        return null;
    }

    /**
     * 重新加载app
     * @param appModel
     */
    public static void loadAppMpdel(AppModel appModel){
        if(appModel == null){
            return;
        }
        ClazzScanUtils.onload(appModel.getPath(),appModel.getAppJarName());
    }

    /**
     * 应用构建
     * @return
     */
    private static AppModel build(String id,String appName,String appJarName,String author,String desc,String path){
        AppModel appModel=new AppModel();
        appModel.setAppId(id);
        appModel.setAppName(appName);
        appModel.setAppJarName(appJarName);
        appModel.setAuthor(author);
        appModel.setDesc(desc);
        appModel.setPath(path);
        return appModel;
    }

}
